package org.jeecg.chase.common.utils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author leikai
 * @version 1.0
 * @description: 驼峰转换
 * @date 2021/07/26/0026 15:16
 */
public class CamelUtils {
    private static Pattern linePattern = Pattern.compile("_(\\w)");
    private static Pattern humpPattern = Pattern.compile("[A-Z]");

    /**
     * 驼峰字段转下划线
     *
     * @param param
     * @return java.lang.String
     * @author leikai
     * @date 2021/07/26/0026 15:17
     */
    public static String camelToUnderline(String param) {
        if (param == null || "".equals(param.trim())) {
            return "";
        }
        int len = param.length();
        StringBuilder str = new StringBuilder(len);
        for (int i = 0; i < len; i++) {
            char c = param.charAt(i);
            if (Character.isUpperCase(c)) {
                str.append('_');
                str.append(Character.toLowerCase(c));
            } else {
                str.append(c);
            }
        }
        return str.toString();
    }

    /**
     * 下划线转驼峰
     *
     * @param str
     * @return java.lang.String
     * @author leikai
     * @date 2021/07/26/0026 15:17
     */
    public static String underlineToCamel(String str) {
        str = str.toLowerCase();
        Matcher matcher = linePattern.matcher(str);
        StringBuffer sb = new StringBuffer();
        while (matcher.find()) {
            matcher.appendReplacement(sb, matcher.group(1).toUpperCase());
        }
        matcher.appendTail(sb);
        return sb.toString();
    }
}
